class DesignerWorkItemsController < ApplicationController

  def index
    @stage = Stage.find(params[:stage_id])
    @type = WorkItemType.find(params[:work_item_type_id])
    @designer = Designer.find(params[:designer_id])
  end
  
  def new
    @stage = Stage.find(params[:stage_id])
    @type = WorkItemType.find(params[:work_item_type_id])
    @designer = Designer.find(params[:designer_id])
    @designer_work_item = DesignerWorkItem.new(
      :stage_id => @stage.id,
      :work_item_type_id => @type.id,
      :designer_id => @designer.id
    )
  end

  def create
    @designer_work_item = DesignerWorkItem.new(params[:designer_work_item])
    @stage = @designer_work_item.work_item.stage
    @type = @designer_work_item.work_item.work_item_type
    @designer = @designer_work_item.designer
    render(:new) unless @designer_work_item.save 
  end

  def edit
    @designer_work_item = DesignerWorkItem.find(params[:id])
    @stage = @designer_work_item.stage
    @type = @designer_work_item.work_item_type
    @designer = @designer_work_item.designer
  end

  def update
    @designer_work_item = DesignerWorkItem.find(params[:id])
    @stage = @designer_work_item.stage
    @type = @designer_work_item.work_item_type
    @designer = @designer_work_item.designer
    render :edit unless @designer_work_item.update_attributes(params[:designer_work_item])
  end

  def destroy
    @designer_work_item = DesignerWorkItem.find(params[:id])
    @stage = @designer_work_item.stage
    @type = @designer_work_item.work_item_type
    @designer = @designer_work_item.designer
    @designer_work_item.destroy    
  end
  
  def new_in_work_item
    @stage = Stage.find(params[:stage_id])
    @type = WorkItemType.find(params[:work_item_type_id])
    designers
  end

  def create_in_work_item
    @designer_work_item = DesignerWorkItem.new(params[:designer_work_item])
    @designer_work_item.save(false)
    @stage = @designer_work_item.stage
    @type = @designer_work_item.work_item_type
    designers
  end

  private
  def designers
    params[:department_id] ||= Department.first.id
    @designers = Designer.find_all_by_department_id(params[:department_id])
  end

end
